<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi

    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

/**
 * fixnotes actions.
 *
 * @package    FinalsClub
 * @subpackage fixnotes
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class fixnotesActions extends sfActions {
	/**
	 * Retreive all notes
	 * @param $request
	 * @return sfViewSuccess
	 */
	public function executeIndex(sfWebRequest $request) {
		$page = $request->getParameter('page', 1);
		$c = new Criteria();
		$c->addDescendingOrderByColumn("course_id");
		$c->add(NotePeer::TAGS,"todo");
		$pager = new sfPropelPager('Note', 50);
		$pager->setPage($page);
		$pager->setCriteria($c);
		$pager->setPeerMethod('doSelectJoinAll');
		$pager->init();
		$this->notes_pager = $pager;
	}
	
	public function executeEdit(sfWebRequest $request) {
		$this->note = NotePeer::retrieveById($request->getParameter('note_id'));
		$term = $request->getParameter('term');
		$page = $request->getParameter('page', 1);
		$c = new Criteria();
		if($term) {
			$c->add(CoursePeer::NAME, "%$term%", Criteria::LIKE);
		}
		$pager = new sfPropelPager('Course', 50);
		$pager->setPage($page);
		$pager->setCriteria($c);
		$pager->setPeerMethod('doSelectJoinSubject');
		$pager->init();
		$this->courses_pager = $pager;
	}
	
	public function executeUpdate(sfWebRequest $request) {
		$note = NotePeer::retrieveById($request->getParameter('note_id'));
		$delete = NotePeer::retrieveById($request->getParameter('delete_id'));
		$course_id = $request->getParameter('course_id');
		$old_course_id = $note->getCourseId();
		if($delete){
			$delete->delete();
		}
		else if($note && $course_id) {
			echo "bool check";
			$c = new Criteria();
			$c->add(NotePeer::COURSE_ID, $old_course_id);
			$c->add(NotePeer::TAGS,"todo");
			$notes = NotePeer::doSelect($c);
			foreach($notes as $current ){
				echo "Setting note "+$current->getId();
				$current->setCourseId($course_id);
				$current->setTags("");
				$current->save();
			}
			$this->getUser()->setFlash('success', 'Your course note has been successfully updated for id: '. $note->getId());
		}
		$page = $request->getParameter('notes_page', 1);
		$this->redirect('fixnotes/index?page='.$page);
	}
	
	public function executeNew(sfWebRequest $request){
		$this->form = new CourseForm(null);
	}
	
	public function executeCreate(sfWebRequest $request){
			
		$course_info = $request->getParameter("course");
		$course_info['user_id'] = ($this->getUser()->getId() ? $this->getUser()->getId() : 1 );
		$course_form = new CourseForm();
		$course_form->bind($course_info);
		print_r($course_info);
		if($course_form->isValid()){
			$course = $course_form->updateObject();
			$course->save();
			$this->getUser()->setFlash("success","Course Created");
			$this->redirect(url_for("fixnotes/new"));	
		}else{
			echo "Form not valid";
		}	
	}
}
